home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 171 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.2 KB  |  73 lines

  1. Path: fido.asd.sgi.com!austern
  2. From: "john (j.d.) hickin" <hickin@bnr.ca>
  3. Newsgroups: comp.std.c++
  4. Subject: (no subject)
  5. Date: 30 Jan 1996 09:51:25 PST
  6. Organization: Bell-Northern Research
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4elfbq$21r@bmtlh10.bnr.ca>
  9. NNTP-Posting-Host: isolde.mti.sgi.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Original-Date:  Tue, 30 Jan 1996 16:00:58 +0000 
  14. Content-Identifier:  (no subject) 
  15. X-Mailer: Mozilla 1.1 (X11; I; HP-UX A.09.05 9000/715) 
  16. X-Url: news:comp.std.c++ 
  17. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  18.     iQBVAwUBMQ5asUy4NqrwXLNJAQH0dQIAixE2FwNm9WPIaZ7cVyXjYzumYDMs42Dq
  19.     ockk8f5kvJQCrFNXX1+fvwyyTjUnkVQjSMjWniSMaY6XnzLf8k992w==
  20.     =EZwq
  21. Originator: austern@isolde.mti.sgi.com
  22.  
  23. Some current compilers allocate memory for heap objects from inside
  24. the constructor code.  This isn't great when exceptions can be thrown
  25. and indeed generated code from compilers that support exception handling
  26. seems to abandon the idea.
  27.  
  28. Does the standard say anything about how memory should be allocated?
  29. My question is motivated by the following simple program which exhibits
  30. different behavior when exception handling is enabled compared to when
  31. it isn't.  Would such behavior be accptable inder the provisions of the
  32. standard?
  33.  
  34. // -----------------------------------------------------------------
  35.  
  36. #include <stddef.h>
  37.  
  38. struct Y
  39. {
  40.   Y() {}
  41.   void* allocate() { return 0; }
  42. };
  43.  
  44. struct X
  45. {
  46.    X() {}
  47.    void* operator new( size_t ) { return (void*)42; }
  48.    void* operator new( size_t , Y& alloc ) { return alloc.allocate(); }
  49.    void  operator delete( void* ) {}
  50.    //void operator delete( size_t , Y& ); // not yet supported by my compiler
  51. };
  52.  
  53.  
  54. #include <iostream.h>
  55.  
  56. main()
  57. {
  58.   Y y;
  59.   X* ptr = new(y) X;
  60.   cout << "ptr == " << (int)ptr << "\n";
  61. }
  62.  
  63. The output is 0 when exception handling is enabled and 42 when it isn't.
  64.  
  65.  
  66. -- 
  67. John Hickin      Bell-Northern Research, Montreal, Quebec
  68. (514) 765-7924   hickin@bnr.ca
  69. ---
  70. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  71.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  72.   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
  73.